package com.tanhua.server.interceptor;

import com.tanhua.model.domain.User;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 工具类：实现向threadlocal存储数据的方法
 */
public class UserHolder {

    static Log log = LogFactory.getLog(UserHolder.class);
    private static ThreadLocal<User> tl = new ThreadLocal<>();


    //将用户对象，存入Threadlocal
    public static void set(User user) {
        String name = Thread.currentThread().getName();
        log.debug("设置用户信息的线程："+name);
        tl.set(user);
    }

    //从当前线程，获取用户对象
    public static User get() {
        return tl.get();
    }

    //从当前线程，获取用户对象的id
    public static Long getUserId() {
        String name = Thread.currentThread().getName();
        log.debug("h获取用户信息的线程："+name);
        if(tl.get()==null){
            return null;
        }
        return tl.get().getId();
    }

    //从当前线程，获取用户对象的手机号码
    public static String getMobile() {
        return tl.get().getMobile();
    }

    //清空
    public static void remove() {
        tl.remove();
    }
}
